package cn.itcast.crm.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.itcast.crm.dao.CustomerRepository;
import cn.itcast.crm.domain.Customer;
import cn.itcast.crm.service.CustomerService;
@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {
	
	@Autowired
	CustomerRepository customerRepository;

	@Override
	public List<Customer> findNoAssociationCustomers() {
		// TODO Auto-generated method stub
		return customerRepository.findByFixedAreaIdIsNull();
	}

	@Override
	public List<Customer> findHasAssociationFixedAreaCustomers(
			String fixedAreaId) {
		// fixedAreaId is ?
		return customerRepository.findByFixedAreaId(fixedAreaId);
	}

	@Override
	public void associationCustomersToFixedArea(String customerIdStr, String fixedAreaId) {
		//解除关联 将定去id置为null
		customerRepository.clearFixedAreaId(fixedAreaId);
		
		if (StringUtils.isBlank(customerIdStr)) {
			return;
		}
		
		//切割字符串
		String[] customerIdArray = customerIdStr.split(",");
		for (String idStr : customerIdArray) {
			int id = Integer.parseInt(idStr);
			customerRepository.updateFixedAreaId(fixedAreaId,id);
			
			
		}
		
		
		
	}

	@Override
	public void regist(Customer customer) {
		customerRepository.save(customer);
		
	}

	@Override
	public Customer findByTelephone(String telephone) {
		return customerRepository.findByTelephone(telephone);
	}

	@Override
	public void updateType(String telephone) {
		
		customerRepository.updateType(telephone);
	}

	@Override
	public Customer findByTelephoneAndPassword(String telephone, String password) {
		return customerRepository.findByTelephoneAndPassword(telephone,password);
	}

	@Override
	public String findFixedByCustomer_idAndAddress(String address, Integer customer_id) {
		return customerRepository.findFixAreaIdByAddressAndID(address,customer_id);
	}

}










